﻿
using Admin.NET.Core.Fitter;
using Org.BouncyCastle.Tls.Crypto.Impl;
using System.Collections.Generic;

namespace Admin.NET.Application.Service;

/// <summary>
/// 前端表单配置
/// </summary>
public class BasFormConfigService : BaseServiceString<BasFormConfig>
{

    public BasFormConfigService(SqlSugarRepository<BasFormConfig> rep) : base(rep)
    {
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    public List<BasFormConfigOutput> All()
    {
        var list = _rep.GetList();
        var output = new List<BasFormConfigOutput>();
        foreach (var item in list)
        {
            var obj = new BasFormConfigOutput()
            {
                Id = item.Id,
                tag = item.tag,
                title = item.title,
                config = item.config
            };
            output.Add(obj);
        }
        return output;
    }

    /// <summary>
    /// 更新成功后返回true
    /// </summary>
    public bool Save(BasFormConfigInput input)
    {
        var hasValue = Queryable.Where(it => it.Id == input.Id).Any();
        if (hasValue)
        {
            return _rep.Update(NewEntity(input));
        }
        else
        {
            _rep.InsertReturnEntity(NewEntity(input));
            return true;
        }
    }

    private BasFormConfig NewEntity(BasFormConfigInput input)
    {
        var entity = new BasFormConfig() { Id = input.Id, tag = input.tag, title = input.title, config = input.config };
        return entity;
    }
}
